// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import LanguageMng from "../../Managers/LanguageMng";

cc.Class({
    extends: cc.Component,

    properties: {

        labelMobile:{
            default:null,
            type:cc.EditBox
        },

        labelOTP:{
            default:null,
            type:cc.EditBox
        },

        optBtn:{
            default:null,
            type:cc.Button,
        },

        otpBtnLabel:{
            default:null,
            type:cc.Label
        }

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this._sendedMobile = []

        cc.gg.gameDataUtil.addKeyboardUpCb("bindmobileup",function () {
            this.node.y -= 200
            // setTimeout(function () {
            //     this.introduceNode.active = false
            //     this.allLoginNode.y -= 500
            // }.bind(this),1000)
        }.bind(this))

        cc.gg.gameDataUtil.addKeyboardDownCb("bindmobiledown",function () {
            this.node.y += 200
            // setTimeout(function () {
            //     this.introduceNode.active = true
            //     this.allLoginNode.y += 500
            // }.bind(this),1000)
        }.bind(this))
    },

    onDestroy(){
        cc.gg.gameDataUtil.removeKeyboardUpCb("bindmobileup")
        cc.gg.gameDataUtil.removeKeyboardDownCb("bindmobiledown")
    },

    start () {

    },

    update (dt) {
        cc.log(cc.winSize.height)
    },

    initBindMobileWindow:function (cb) {
        this.successCb = cb
    },

    showBindMobileWindow:function () {
        // this.labelMobile.string = ""
        // this.labelOTP.string = ""

        // if(this._counting){
        //     this._counting = false
        //     this.stopOtpCountDown(LanguageMng.getSentence(194))
        // }else {
        //     this.otpBtnLabel.string = LanguageMng.getSentence(194)
        // }
    },

    onBindMobileCloseClicked:function (event) {
        this.node.destroy()
    },

    onBindMobileSendOTPClicked:function (event) {
        let mobileStr = this.labelMobile.string

        if(mobileStr.indexOf("91") !== 0){
            mobileStr = "91" + mobileStr
        }

        if(!mobileStr || mobileStr === ""){
            cc.gg.toastMgr.showToast(LanguageMng.getSentence(190))
            return
        }

        if(!this.checkMobile(mobileStr)){
            cc.gg.toastMgr.showToast(LanguageMng.getSentence(195))
            return
        }

        this.startOtpCountDown(60)


        if(this._numberSendedBefore(mobileStr)){
            cc.log("resend otp to :",mobileStr)
            cc.gg.httpUtil.httpPost("/user/pin/resent",{mobileNo:mobileStr, pinId:this.pinId},function (result) {
                if (!result.succ) {
                    cc.gg.toastMgr.showToast("otp send failed.")
                    cc.error(`验证码发送失败: ${result.msg}`);
                    return;
                }

                let pinId = result.data.pinId
                if(pinId) this.pinId = pinId
                cc.log("pincode::::::::::::::::",result,"  ",result.data.pinId)
            }.bind(this))
        }else {
            cc.log("Send otp to :",mobileStr)
            cc.gg.httpUtil.httpPost("/user/pin/send",{mobileNo:mobileStr},function (result) {
                if (!result.succ) {
                    cc.gg.toastMgr.showToast("otp send failed.")
                    cc.error(`验证码发送失败: ${result.msg}`);
                    return;
                }

                cc.gg.toastMgr.showToast(LanguageMng.getSentence(196))
                if(!this._numberSendedBefore(mobileStr)){
                    this._sendedMobile.push(mobileStr)
                }
                this.pinId = result.data.pinId
                cc.log("pincode::::::::::::::::",result.data.pinId)
            }.bind(this))
        }
    },

    _numberSendedBefore:function (number) {
        for(let i=0;i<this._sendedMobile.length;i++){
            let n = this._sendedMobile[i]
            if(n === number)
                return true
        }
        return false
    },

    checkMobile:function (mobileNo) {
        mobileNo = mobileNo.replace(/ /g, '');
        let regx = /^91\d{10}$/;
        if (!regx.test(mobileNo)) return false
        return true
    },

    startOtpCountDown:function (seconds) {
        this._counting = true
        this.maxCount = seconds
        this.otpBtnLabel.node.color = new cc.Color(200,200,200)
        this.optBtn.interactable = false
        this.countDownHandler = function () {
            this.maxCount--
            if(this.maxCount === 0){
                this._counting = false
                this.stopOtpCountDown(LanguageMng.getSentence(193))
                return
            }
            this.otpBtnLabel.string = "Send again("+this.maxCount+")"
        }.bind(this)
        this.schedule(this.countDownHandler, 1)
    },

    stopOtpCountDown:function (btnStr) {
        this.optBtn.interactable = true
        this.otpBtnLabel.string = btnStr
        this.otpBtnLabel.node.color = new cc.Color(255,255,255)
        if(this.countDownHandler)
            this.unschedule(this.countDownHandler)
    },

    doBindMobile:function (callback) {
        let otpStr = this.labelOTP.string
        if(!otpStr || otpStr === ""){
            cc.gg.toastMgr.showToast(LanguageMng.getSentence(191))
            return
        }

        let mobileStr = this.labelMobile.string
        if(mobileStr.indexOf("91") !== 0){
            mobileStr = "91" + mobileStr
        }
        if(!this.checkMobile(mobileStr)){
            cc.gg.toastMgr.showToast(LanguageMng.getSentence(195))
            return
        }

        cc.gg.httpUtil.httpPost("/user/profile/mobile/bind",{pinId:this.pinId,pinCode:otpStr},function (result) {
            if (!result.succ) {
                if(result.code == 5){
                    cc.gg.toastMgr.showToast(LanguageMng.getSentence(189))  //MobileNo Has Already Registered
                }else
                    cc.gg.toastMgr.showToast(result.msg)
                return;
            }

            if(callback){
                callback()
            }
        }.bind(this))
    },

    onBindMobileBindClicked:function (event) {
        this.doBindMobile(function () {
            this.node.destroy()
            this._showSuccessDialog()
            if(this.successCb) this.successCb()
        }.bind(this))
    },

    _showSuccessDialog:function () {
        let title = LanguageMng.getSentence(3)
        let btnStr = LanguageMng.getSentence(57)
        let contentStr = LanguageMng.getSentence(181)
        let cb = function () {
            cc.gg.toastMgr.removeDialog()
        }.bind(this)
        cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cb)
    }
});
